const stringIsEmpty = function (obj){
    if(typeof obj == "undefined" || obj == null || obj == ""){
        return true;
    }else{
        return false;
    }
}
const isGetAutoSettion = function (a="scope.userInfo"){ // 3. 检查当前是否已经授权访问scope属性
		return new Promise(function(resolve, reject) {
			uni.getSetting({
				success: function(res) {
					console.log(res);
					console.log(a);
					if (res.authSetting[a]) {
						console.log('1存在');
						console.log(res);
						
						resolve(true);
					} else {
						console.log('1不存在');
						resolve(false);
					}
				}
			})
		}).catch((e) => {
			console.log(e)
		});
}

const getUserInfo = function(e){

	console.log("eeeeee333");
	console.log(e.detail.userInfo);
	let that = this;
	var p = this.isGetAutoSettion('scope.userInfo');
	p.then(function(isAuth){
		if(isAuth){
			uni.login({
				success:(res)=>{
					if(res.errMsg == "login:ok"){
						let userObject = e.detail.userInfo;
						let userString = userObject.nickName+"___"+userObject.gender+"___"+userObject.city+"___"+userObject.province+"___"+userObject.avatarUrl;
						console.log(userString);
						uni.request({
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							// url: 'https://st.lingyuncmy.com/wxlogin/'+res.code+"/"+encodeURIComponent(userString),
							url:"http://st.lingyuncmy.com/index.php/api/user/wxlogin",
							method: 'POST',//接口类型 
							data: {
								'code':res.code,
								'avatar_url':userObject.avatarUrl,
								'city':userObject.city,
								'province':userObject.province,
								'gender':userObject.gender,
								'nick_name':userObject.nickName
							}, //接口需要的数据
							success: function(res) {
								console.log(res);
								if (res.data.code == 1) {
									uni.setStorageSync('openid', res.data.data.openid);
									// that.login(res.data); //将接口返回的数据保存在全局变量中.
									//登录成功跳转首页或者你想跳转的地方... 
									//注意：如果时导航页 请用uni.switchTab
									// 		其他页面建议使用uni.reLaunch
								} else {
									uni.showToast({
										title: '授权登录失败！',
										mask: true,
										icon: 'none'
									})
								}
										
										
							}
										
						})
						
					}
					
				}
			})
		} else {
			console.log("unils没有获取授权");
			uni.authorize({
				scope: 'scope.userInfo',
				success() {
					// console.log('33333');
					uni.getUserInfo();
				}
			})
		}
	})
}

module.exports = {
	stringIsEmpty,
	isGetAutoSettion,
	getUserInfo
}